package org.quasar.airNova.presentationLayer;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.NClob;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.quasar.airNova.businessLayer.CalendarDate;
import org.quasar.airNova.businessLayer.Reserva;

public class TrocarAviao extends JPanel {

	private static final long serialVersionUID = 1L;
	private Gui gui;
	private JComboBox aviDisponiveis;
	private JComboBox nVoo;
	private JComboBox cbDatas;
	private JTextField aviAtual;

	/**
	 * Create the panel.
	 * 
	 * @param gui
	 */
	public TrocarAviao(Gui gui) {
		setGui(gui);
		setLayout(new BorderLayout(0, 0));
		this.setSize(500, 350);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("Voo");
		lblNewLabel.setBounds(40, 76, 151, 14);
		panel.add(lblNewLabel);

		JLabel lblNewLabel_2 = new JLabel("Avioes Disponiveis");
		lblNewLabel_2.setBounds(40, 148, 126, 14);
		panel.add(lblNewLabel_2);

		JLabel Aviao = new JLabel("Aviao Atual");
		Aviao.setBounds(40, 123, 126, 14);
		panel.add(Aviao);

		JButton btnNewButton = new JButton("Submeter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(nVoo.getSelectedItem() != null && cbDatas.getSelectedItem() != null && aviDisponiveis.getSelectedItem() != null)
				AirNova_Database.trocarAviao((String) nVoo.getSelectedItem(),
						(String) cbDatas.getSelectedItem(),
						(String) aviDisponiveis.getSelectedItem());
			}
		});
		btnNewButton.setBounds(186, 217, 98, 23);
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Cancelar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getTabbedPane().removeTabAt(
						getGui().getTabbedPane().getSelectedIndex());
			}
		});
		btnNewButton_1.setBounds(294, 217, 89, 23);
		panel.add(btnNewButton_1);

		JLabel lblNewLabel_3 = new JLabel("Trocar avi\u00E3o");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel_3.setBounds(30, 24, 243, 32);
		panel.add(lblNewLabel_3);
		cbDatas = new JComboBox();
		cbDatas.setBounds(231, 95, 203, 20);
		panel.add(cbDatas);
		
		if(Tools.getVoosNumero() != null) {
			nVoo = new JComboBox(Tools.getVoosNumero());
			generateComboxData((String) nVoo.getSelectedItem());
		}
		else {
			nVoo = new JComboBox();
		}
		nVoo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				generateComboxData((String) nVoo.getSelectedItem());
			}
		});
		nVoo.setBounds(231, 73, 203, 20);
		panel.add(nVoo);

		JLabel datas = new JLabel("Datas");
		datas.setBounds(40, 98, 151, 14);
		panel.add(datas);

		
		if(Tools.getAviaoByVooAndCalendarDate(
				(String) nVoo.getSelectedItem(),
				(String) cbDatas.getSelectedItem()) != null)
		aviAtual = new JTextField(Tools.getAviaoByVooAndCalendarDate(
				(String) nVoo.getSelectedItem(),
				(String) cbDatas.getSelectedItem()));
		else
			aviAtual = new JTextField();
		aviAtual.setEditable(false);
		aviAtual.setBounds(231, 120, 203, 20);
		panel.add(aviAtual);
		aviAtual.setColumns(10);

		aviDisponiveis = new JComboBox();
		aviDisponiveis.setBounds(231, 145, 203, 20);
		panel.add(aviDisponiveis);
		generateAvioesDisponiveis();

	}

	private void generateAvioesDisponiveis() {
		String[] stringAvioes = Tools.getAllAvioes();

		aviDisponiveis.removeAllItems();
		for (int i = 0; i < stringAvioes.length; i++) {
			aviDisponiveis.addItem("" + stringAvioes[i]);
		}

	}

	public Gui getGui() {
		return gui;
	}

	private void generateComboxData(String nVoo) {
		CalendarDate[] cds = Tools.getVoosData(nVoo);
		addDatasComboBox(cds);
	}

	private void addDatasComboBox(CalendarDate[] cds) {
		cbDatas.removeAllItems();
		for (int i = 0; i < cds.length; i++) {
			cbDatas.addItem("" + cds[i].day() + "-" + cds[i].month() + "-"
					+ cds[i].year());
		}
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}
}
